/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 3/22/14
 * Time: 7:06 PM
 * To change this template use File | Settings | File Templates.
 */
package com.societyGames.codeAlong.state.codeExercise.model.testCase
{
import flash.events.TimerEvent;
import flash.utils.Timer;

public class DelayState extends TestCaseControllerState implements IStoppable
{
  private var _timer:Timer;

  public function DelayState(testCaseController:TestCaseController)
  {
    super(testCaseController);
    _timer = new Timer(500, 1);
    _timer.addEventListener(TimerEvent.TIMER, handleTimer);
  }

  override public function enter():void
  {
    _timer.reset();
    _timer.start();
  }

  private function handleTimer(timerEvent:TimerEvent):void
  {
    _timer.stop();

    var nextTestCaseIndex:int = _testCaseController.currentTestCaseIndex + 1;
    if (nextTestCaseIndex >= _testCaseController._testCases.length)
    {
      nextTestCaseIndex = 0;
    }
    _testCaseController._selectCase(nextTestCaseIndex);

    _testCaseController._stateMachine.gotoState(_testCaseController._waitForCanExecuteState);
  }

  public function stop():void
  {
    _timer.stop();
    _testCaseController._stateMachine.gotoState(_testCaseController._failState); //TODO: Should this be StoppedState?
  }
}
}
